<template>
  <div id="app">
    <nav class="navbar">
      <div class="logo">
        <img src="./assets/logo.png" alt="Website Logo" class="logo-img" />
        <span class="logo-text">聊天交友网站</span>
      </div>
      <div class="nav-links">
        <router-link to="/" class="nav-link">首页</router-link>
        <router-link to="/community" class="nav-link">社区</router-link>
        <router-link to="/vip" class="nav-link">VIP</router-link>
        <router-link to="/recommend" class="nav-link">推荐</router-link>
        <router-link to="/chat" class="nav-link">我的消息</router-link>
      </div>
      
      <div class="user-actions">
        <el-button 
          v-if="!userStore.isLogin" 
          @click="handleLoginClick" 
          type="primary"
        >
          登录
        </el-button>
        <el-dropdown 
          v-else 
          @command="handleDropdownCommand"
        >
          <div class="user-avatar-name">
            <el-avatar
              :src="userStore.avatar"
              size="medium"
              class="avatar"
            />
            <span class="username">{{ userStore.username }}</span>
          </div>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="logout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </nav>
    <router-view />
  </div>
</template>

<script setup>
import { useUserStore } from './stores/user'
import { useRouter } from 'vue-router'

const userStore = useUserStore()
const router = useRouter()

console.log('userStore after login:', userStore); 

const handleLoginClick = () => {
  router.push('/login')
}

const handleDropdownCommand = (command) => {
  if (command === 'logout') {
    userStore.logout()
  }
}
</script>

<style scoped>
.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 20px;
  background-color: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.logo {
  display: flex;
  align-items: center;
}

.logo-img {
  width: 40px;
  height: 40px;
  margin-right: 10px;
}

.logo-text {
  font-size: 18px;
  font-weight: bold;
}

.nav-links {
  display: flex;
}

.nav-link {
  margin-right: 20px;
  text-decoration: none;
  color: #333;
  font-size: 16px;
}

.user-actions {
  display: flex;
  align-items: center;
}

.user-actions.el-button {
  margin-right: 10px;
}

.user-avatar-name {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.avatar {
  margin-right: 8px;
}

.username {
  font-size: 16px;
  color: #333;
}
</style>